<?php
	session_start();
	mysql_connect("localhost", "root", "");
	$action = $_GET['action'];
	
	//drop and create database, the connect to database
	mysql_query('drop database if exists ps3_theatre;') or die(mysql_error());
	mysql_query('create database ps3_theatre;') or die(mysql_error());
	mysql_select_db("ps3_theatre");

	//Table structure for table `tbl_audio_flags`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_audio_flags` (
					  `id` int(6) NOT NULL AUTO_INCREMENT,
					  `label` varchar(255) NOT NULL,
					  `value` varchar(255) NOT NULL,
					  PRIMARY KEY (`id`)
					) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3;") or die(mysql_error());
					
	//Dumping data for table `tbl_audio_flags`
	mysql_query("INSERT INTO `tbl_audio_flags` (`id`, `label`, `value`) VALUES
					(1, 'Stereo 2.0', 'dolby20'),
					(2, 'Dolby 5.1', 'dolbydigital');") or die(mysql_error());

	//Table structure for table `tbl_cacheimage`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_cacheimage` (
	 				`id` int(6) NOT NULL AUTO_INCREMENT,
  					`type_id` int(6) NOT NULL,
  					`link_id` int(6) NOT NULL,
  					`keyword` varchar(512) NOT NULL,
  					`subfolder_path` varchar(1024) NOT NULL,
  					`filename` varchar(512) NOT NULL,
  					PRIMARY KEY (`type_id`,`keyword`,`id`),
  					KEY `id` (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());

	//table cacheimage intentionally left blank
						
	//Table structure for table `tbl_directories`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_directories` (
				  `id` int(6) NOT NULL AUTO_INCREMENT,
				  `type_id` int(6) NOT NULL,
				  `computer_path` varchar(512) NOT NULL,
				  `http_path` varchar(512) NOT NULL,
				  PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());

	//table directories intentionally left blank

	//Table structure for table `tbl_disc_flags`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_disc_flags` (
				  `id` int(6) NOT NULL AUTO_INCREMENT,
				  `label` varchar(255) NOT NULL,
				  `value` varchar(255) NOT NULL,
				  PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;") or die(mysql_error());

	//Dumping data for table `tbl_disc_flags`
	mysql_query("INSERT INTO `tbl_disc_flags` (`id`, `label`, `value`) VALUES
					(1, 'Blu-Ray', 'bluray'),
					(2, 'DivX', 'divx'),
					(3, 'DVD', 'dvd'),
					(4, 'HD-DVD', 'hddvd'),
					(5, 'HDTV', 'hdtv'),
					(6, 'MP3', 'mp3'),
					(7, 'Standard Def', 'sd');") or die(mysql_error());
										
	//Table structure for table `tbl_errorstrings`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_errorstrings` (
  					`id` int(6) NOT NULL AUTO_INCREMENT,
  					`error_string` varchar(512) NOT NULL,
  					PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;") or die(mysql_error());

	//Dumping data for table `tbl_errorstrings`
	mysql_query("INSERT INTO `tbl_errorstrings` (`id`, `error_string`) VALUES
					(1, 'More than 1 movie result returned, adding year to filename might help return fewer results.  Click rescan and select the correct movie.'),
					(2, 'No Results returned. Title may be mispelled.'),
					(3, 'Invalid file name.  Name needs to have series title and (S##E##)'),
					(4, 'Series couldn''t be found'),
					(5, 'Episode information couldn''t be found'),
					(6, 'More than 1 series result returned.');") or die(mysql_error());
					
	//Table structure for table `tbl_fileextensions`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_fileextensions` (
				  `id` int(6) NOT NULL AUTO_INCREMENT,
				  `type_id` int(6) NOT NULL,
				  `extension` varchar(255) NOT NULL,
				  `is_active` char(1) NOT NULL,
				  PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=27;") or die(mysql_error());

	//Dumping data for table `tbl_fileextensions`
	mysql_query("INSERT INTO `tbl_fileextensions` (`id`, `type_id`, `extension`, `is_active`) VALUES
					(1, 1, 'mpg', 'Y'),
					(2, 1, 'mpeg', 'Y'),
					(3, 1, 'avi', 'Y'),
					(4, 1, 'm2ts', 'Y'),
					(5, 1, 'm4v', 'Y'),
					(6, 1, 'mkv', 'N'),
					(7, 1, 'mp4', 'Y'),
					(8, 1, 'h264', 'Y'),
					(9, 2, 'mpg', 'Y'),
					(10, 2, 'mpeg', 'Y'),
					(11, 2, 'avi', 'Y'),
					(12, 2, 'm2ts', 'Y'),
					(13, 2, 'm4v', 'Y'),
					(14, 2, 'mkv', 'N'),
					(15, 2, 'mp4', 'Y'),
					(16, 2, 'h264', 'Y'),
					(17, 3, 'oma', 'Y'),
					(18, 3, 'msa', 'Y'),
					(19, 3, 'aa3', 'Y'),
					(20, 3, 'oma', 'Y'),
					(21, 3, 'msa', 'Y'),
					(22, 3, 'aa3', 'Y'),
					(23, 3, '3gp', 'Y'),
					(24, 3, 'mp4', 'Y'),
					(25, 3, 'mp3', 'Y'),
					(26, 3, 'wav', 'Y');") or die(mysql_error());
					
	//Table structure for table `tbl_movies`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_movies` (
				  `id` int(6) NOT NULL AUTO_INCREMENT,
				  `directory_id` int(6) NOT NULL,
				  `subfolder_path` varchar(1024) NOT NULL,
				  `filename` varchar(512) NOT NULL,
				  `tmdb_id` int(10) NOT NULL,
  				  `error_id` int(6) NULL,
				  `title` varchar(512) NOT NULL,
				  `overview` varchar(1024) NOT NULL,
				  `rating` double NOT NULL,
				  `certification` varchar(10) NOT NULL,
				  `released_date` date NOT NULL,
				  `runtime` int(10) NOT NULL,
				  `budget` int(20) NOT NULL,
				  `revenue` int(20) NOT NULL,
				  `director` varchar(512) NOT NULL,
				  `disc_flag_id` int(6) NOT NULL,
				  `audio_flag_id` int(6) NOT NULL,
				  `is_watched` char(1) NOT NULL,
				  PRIMARY KEY (`id`,`directory_id`,`filename`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());
	
	//tbl_movies intentionally left blank
	
	//Table structure for table `tbl_movies_settings`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_movies_settings` (
				  `theme_id` int(6) NOT NULL
				) ENGINE=MyISAM DEFAULT CHARSET=latin1;") or die(mysql_error());
				
	//tbl_movies_settings intentionally left blank
	
	//Table structure for table `tbl_settings`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_settings` (
				  `version` varchar(64) NOT NULL,
				  `theme` varchar(512) NOT NULL,
				  `move_the` char(1) NOT NULL
				) ENGINE=MyISAM DEFAULT CHARSET=latin1;") or die(mysql_error());

	//Dumping data for table `tbl_settings`
	mysql_query("INSERT INTO `tbl_settings` (`version`, `theme`, `move_the`) VALUES
					('V0.3 06/29/2011', '', 'Y');") or die(mysql_error());

	//Table structure for table `tbl_themes`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_themes` (
				  `id` int(6) NOT NULL AUTO_INCREMENT,
				  `name` varchar(30) NOT NULL,
				  `type_id` int(6) NOT NULL,
				  `url` varchar(256) NOT NULL,
				  PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;") or die(mysql_error());

	//Dumping data for table `tbl_themes`
	mysql_query("INSERT INTO `tbl_themes` (`id`, `name`, `type_id`, `url`) VALUES
					(1, 'Movie List - 1080P', 1, 'movies-list1080.php'),
					(2, 'Tv Shows', 2, 'tvShows.php'),
					(3, 'Music', 3, 'music.php'),
					(4, 'Weather', 5, ''),
					(5, 'Random', 6, '');") or die(mysql_error());
				
	//Table structure for table `tbl_tvshow_episode`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_tvshow_episode` (
			  		`id` int(6) NOT NULL AUTO_INCREMENT,
  					`directory_id` int(6) NOT NULL,
  					`subfolder_path` varchar(1024) NOT NULL,
  					`filename` varchar(512) NOT NULL,
  					`tvdbepisode_id` int(10) NOT NULL,
  					`series_id` int(10) NOT NULL,
  					`error_id` int(6) DEFAULT NULL,
  					`season` int(6) NOT NULL,
  					`episode` int(6) NOT NULL,
  					`airdate` varchar(10) NOT NULL,
  					`name` varchar(512) NOT NULL,
	 				`description` varchar(1024) NOT NULL,
					`is_watched` char(1) NOT NULL,
				 	PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());

	//tbl_tvshow_episode intentionally left blank
	
	//Table structure for table `tbl_tvshow_series`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_tvshow_series` (
  					`id` int(6) NOT NULL AUTO_INCREMENT,
  					`tvdbseries_id` int(10) NOT NULL,
  					`name` varchar(512) NOT NULL,
  					`description` varchar(1024) NOT NULL,
  					`content_rating` varchar(10) NOT NULL,
  					`first_aired` varchar(10) NOT NULL,
  					`genre` varchar(256) NOT NULL,
  					`network` varchar(30) NOT NULL,
 					`network_image` varchar(128) NOT NULL,
  					`rating` double NOT NULL,
					`runtime` int(4) NOT NULL,
					PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());

	//tbl_tvshow_series intentionally left blank
	
	//Table structure for table `tbl_types`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_types` (
				  `id` int(6) NOT NULL AUTO_INCREMENT,
				  `type` varchar(30) NOT NULL,
				  `theme_id` int(6) NOT NULL,
				  `order` int(6) NOT NULL,
  				  `displayed` char(1) NOT NULL,
				  PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;") or die(mysql_error());

	//Dumping data for table `tbl_types`
	mysql_query("INSERT INTO `tbl_types` (`id`, `type`, `theme_id`, `order`, `displayed`) VALUES
					(1, 'Movies', 1, 1, 'Y'),
					(2, 'Tv Shows', 2, 2, 'Y'),
					(3, 'Music', 3, 3, 'N'),
					(4, 'Main', 0, 0, 'N'),
					(5, 'Weather', 4, 4, 'Y'),
					(6, 'Random', 5, 5, 'Y');") or die(mysql_error());
					
	//Tabkle structure for table `tbl_wakeonlan`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_wakeonlan` (
					`id` int(6) NOT NULL AUTO_INCREMENT,
  					`mac_address` varchar(17) NOT NULL,
  					`secure_on` varchar(100) NOT NULL,
  					`broadcast_address` varchar(50) NOT NULL,
  					`cidr` int(2) NOT NULL,
  					`port` int(5) NOT NULL,
  					PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());

	//Dumping data for table `tbl_wakeonlan`
	mysql_query("INSERT INTO `tbl_wakeonlan` (`secure_on`, `cidr`, `port`) VALUES
('N', '24', '7');") or die(mysql_error());
	
	//Table structure for table `tbl_weather`
	mysql_query("CREATE TABLE IF NOT EXISTS `tbl_weather` (
  					`zip_code` varchar(10) NOT NULL,
  					`night_mode` char(1) NOT NULL,
  					`night_start` varchar(5) NOT NULL,
  					`night_end` varchar(5) NOT NULL,
  					PRIMARY KEY (`zip_code`)
				) ENGINE=MyISAM DEFAULT CHARSET=latin1;") or die(mysql_error());

	//Dumping data for table `tbl_weather`
	mysql_query("INSERT INTO `tbl_weather` (`zip_code`, `night_mode`, `night_start`, `night_end`) VALUES
('52405', 'Y', '18:00', '06:00');") or die(mysql_error());

	if(isset($_SESSION['version'])){	
		unset($_SESSION['version']);
		unset($_SESSION['moveThe']);
		unset($_SESSION['cacheHttpDir']);
		unset($_SESSION['cacheCompDir']);
	}

	if($action == 'create'){
			header( 'Location: index.php?message=Database sucessfully created' ) ;
	}else if($action == 'recreate'){
		header( 'Location: main-setup.php?message=Database sucessfully created' ) ;
	}
?>